﻿
using IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class BaseDAL<T> : IBaseDAL<T> where T : class
    {
        public BaseDAL(MyDbContext db)
        {
            Db = db;
        }

        public MyDbContext Db { get; }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T t)
        {
            await Db.Set<T>().AddAsync(t);
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<int> AddRangeAsync(List<T> list)
        {
            await Db.Set<T>().AddRangeAsync(list);
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> DeleteAsync(T t)
        {
            Db.Set<T>().Remove(t);
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<int> DeleteRangeAsync(List<T> list)
        {
            Db.Set<T>().RemoveRange(list);
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 预备查询
        /// </summary>
        /// <returns></returns>
        public IQueryable<T> GetValues()
        {
            return Db.Set<T>();
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T t)
        {
            Db.Set<T>().Remove(t);
            return await Db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量修改
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public async Task<int> UpdateRangeAsync(List<T> list)
        {
            Db.Set<T>().UpdateRange(list);
            return await Db.SaveChangesAsync();
        }
    }
}
